package jsf.util;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import jpa.controllers.CoursetagJpaController;
import jpa.controllers.TagJpaController;
import entity.Coursetag;
import entity.Tag;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.tagcloud.DefaultTagCloudItem;
import org.primefaces.model.tagcloud.DefaultTagCloudModel;
import org.primefaces.model.tagcloud.TagCloudModel;

@ManagedBean
@RequestScoped
public class TagCloudBean {

    private TagCloudModel model;
    private CoursetagJpaController coursetagjpa;
    private TagJpaController tagjpa;

    public TagCloudBean() {
        model = new DefaultTagCloudModel();
        coursetagjpa = new CoursetagJpaController();
        tagjpa = new TagJpaController();
    }

    public TagCloudModel getModel() {
        return model;
    }

    public void onSelect(SelectEvent event) {
    }

    public void onUpdate(int course_id) {
        model = new DefaultTagCloudModel();
        Random random = new Random();

        List<Coursetag> coursetag_list = coursetagjpa.findCoursetagEntities();
        List<Tag> tags = new ArrayList<Tag>();

        for (Coursetag coursetag : coursetag_list) {
            if (coursetag.getCourseId().getCourseId() == course_id) {
                tags.add(tagjpa.findTag(coursetag.getTagId().getId()));
            }
        }

        for (Tag tag : tags) {
            model.addTag(new DefaultTagCloudItem(tag.getTagName(), random.nextInt(7)));
        }
    }
}
